package com.yoyo.itravel.service.impl;

import com.yoyo.itravel.bean.*;
import com.yoyo.itravel.dao.IRoleDao;
import com.yoyo.itravel.exceptions.RoleException;
import com.yoyo.itravel.service.IRoleService;
import com.yoyo.itravel.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;


/**
 * @Description: 角色操作服务层接口实现类
 * @Author:CRR
 * @Date：14:35 2020/6/22
 */
@Service
public class IRoleServiceImpl implements IRoleService {

    @Autowired
    private IRoleDao roleDao;

    /**
     * @param role
     * @return
     * @Description: 新增角色
     * @param:
     */
    @Override
    public boolean addRole(Role role) throws RoleException {
        if (role == null) {
            throw new RoleException("role为null");
        }

        try {
            role.setRoleId(StringUtils.getRandomUUID());
            return roleDao.insert(role) == 1;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RoleException("添加失败");
        }

    }

    /**
     * @param roleId
     * @return
     * @Description:根据id删除角色
     * @param:
     */
    @Override
    public boolean deleteByRoleId(String roleId) throws RoleException {
        try {
            return roleDao.deleteByRoleId(roleId) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RoleException("通过id删除失败");
    }

    /**
     * @param page
     * @return cn.hut.travel.bean.PageBean<cn.hut.travel.bean.Role>
     * @Description 分页查询所有角色     *
     */
    @Override
    public List<Role> findAllByPage(PageBean page) throws RoleException {

        if (page == null) {
            return null;
        }
        // 根据选择的条件获得的角色集合
        List<Role> roles = null;
        try {
            roles = roleDao.selectAllByPage(page.getStartPage(), page.getPageSize());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return roles;
    }

    /**
     * @Description 查询角色数据的总条数
     * @Param
     * @Return java.lang.Integer
     */
    @Override
    public Integer findTotalCount() throws RoleException {
        try {
            return roleDao.selectTotalCount();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RoleException("查询角色数据的总条数失败");
        }
    }
}
